import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

class ColorPickerPage extends StatefulWidget {
  const ColorPickerPage({super.key});

  @override
  State<ColorPickerPage> createState() => _ColorPickerPageState();
}

class _ColorPickerPageState extends State<ColorPickerPage> {
  Color currentColor = Colors.black;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('颜色管理')),
      body: Column(
        children: [
          MaterialButton(
            elevation: 3.0,
            onPressed: () {
              final textController = TextEditingController(text: '#FF00FFFF');
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    scrollable: true,
                    titlePadding: const EdgeInsets.all(0.0),
                    contentPadding: const EdgeInsets.all(0.0),
                    content: Column(
                      children: [
                        ColorPicker(
                          pickerColor: currentColor,
                          onColorChanged: (Color color) {
                            setState(() {
                              currentColor = color;
                            });
                          },
                          colorPickerWidth: 300.0,
                          pickerAreaHeightPercent: 0.7,
                          enableAlpha: true,
                          displayThumbColor: true,
                          labelTypes: [],
                          paletteType: PaletteType.hsv,
                          pickerAreaBorderRadius: const BorderRadius.only(
                            topLeft: Radius.circular(2.0),
                            topRight: Radius.circular(2.0),
                          ),
                          hexInputController: textController,
                          portraitOnly: true,
                        ),
                      ],
                    ),
                  );
                },
              );
            },
            color: currentColor,
            textColor: useWhiteForeground(currentColor)
                ? Colors.white
                : Colors.black,
            child: const Text('通过文本输入更改我'),
          ),
        ],
      ),
    );
  }
}
